package liangtWorkSpace.simple;

/**
 * @ClassName LongestCommonPrefix
 * @Description : 最长公共前缀
 * @Author : LiangT
 * @Date : 2020/6/15 18:59
 */
public class LongestCommonPrefix {
    public String longestCommonPrefix(String[] strs) {
        if (strs == null || strs.length < 1){
            return new String();
        }
        String str = strs[0];
        for (int i = 1; i < strs.length; i++){
            str = getStr(str,strs[i]);
        }
        return str;
    }
    public String getStr(String str1,String s2){
        if (str1 == null || str1.length() == 0){
            return str1;
        }
        String str = str1;
        if (!s2.startsWith(str1)){
            str = str1.substring(0,str1.length()-2);
            str = getStr(str,s2);
        }
        return str;
    }

    public static void main(String[] args) {
        String[] strings = new String[]{"flower","flow","flight"};
        LongestCommonPrefix longestCommonPrefix = new LongestCommonPrefix();
        System.out.println(longestCommonPrefix.longestCommonPrefix(strings));
    }
}
